package com.xxc.hs;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.xxc.hs.bean.UserInfo;
import com.xxc.hs.db.UserHelper;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

@SuppressWarnings("ALL")
public class UpdatePwdActivity extends AppCompatActivity {
    private EditText et_newName, et_newNameAgain;
    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_pwd);

        try {
            //初始化控件
            et_newName = findViewById(R.id.et_newName);
            et_newNameAgain = findViewById(R.id.et_newNameAgain);
            toolbar = findViewById(R.id.toolBar_updatePwd);

            //点击事件
            findViewById(R.id.btn_updatePwd).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String name = et_newName.getText().toString();
                    String newName = et_newNameAgain.getText().toString();

                    if (TextUtils.isEmpty(name) || TextUtils.isEmpty(newName)) {
                        Toast.makeText(UpdatePwdActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                    } else if (!name.equals(newName)) {
                        Toast.makeText(UpdatePwdActivity.this, "两次密码输入不正确", Toast.LENGTH_SHORT).show();
                    } else {
                        UserInfo userInfo = UserInfo.getUserInfo();
                        if (null != userInfo) {
                            int i = UserHelper.getInstance(UpdatePwdActivity.this).updatePwd(userInfo.getUsername(), name);
                            if (i > 0) {
                                Toast.makeText(UpdatePwdActivity.this, "修改密码成功", Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(UpdatePwdActivity.this, LoginActivity.class));
                                finish();
                            } else {
                                Toast.makeText(UpdatePwdActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                }
            });

            //返回
            toolbar.setOnClickListener(v -> finish());

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}